﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.Tableaus.ThumbnailCache
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using System.Collections.Generic;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.Tableaus
{
  public class ThumbnailCache
  {
    private int _capacity;
    private ThumbnailCreatorView _thumbnailCreatorView;
    private Dictionary<string, ThumbnailCacheNode> _map;
    private NodeComparer _nodeComparer = new NodeComparer();

    public int Count { get; private set; }

    public ThumbnailCache(int capacity, ThumbnailCreatorView thumbnailCreatorView)
    {
      this._capacity = capacity;
      this._map = new Dictionary<string, ThumbnailCacheNode>(capacity);
      this._thumbnailCreatorView = thumbnailCreatorView;
      this.Count = 0;
    }

    public bool GetValue(string key, out Texture texture)
    {
      texture = (Texture) null;
      ThumbnailCacheNode thumbnailCacheNode;
      if (!this._map.TryGetValue(key, out thumbnailCacheNode))
        return false;
      thumbnailCacheNode.FrameNo = Utilities.EngineFrameNo;
      texture = thumbnailCacheNode.Value;
      return true;
    }

    public bool AddReference(string key)
    {
      ThumbnailCacheNode thumbnailCacheNode;
      if (!this._map.TryGetValue(key, out thumbnailCacheNode))
        return false;
      ++thumbnailCacheNode.ReferenceCount;
      return true;
    }

    public bool MarkForDeletion(string key)
    {
      ThumbnailCacheNode thumbnailCacheNode;
      if (!this._map.TryGetValue(key, out thumbnailCacheNode))
        return false;
      --thumbnailCacheNode.ReferenceCount;
      return true;
    }

    public void ForceDelete(string key)
    {
      ThumbnailCacheNode thumbnailCacheNode;
      if (!this._map.TryGetValue(key, out thumbnailCacheNode))
        return;
      thumbnailCacheNode.ReferenceCount = 0;
    }

    public void Tick()
    {
      if (this.Count <= this._capacity)
        return;
      List<ThumbnailCacheNode> thumbnailCacheNodeList = new List<ThumbnailCacheNode>();
      foreach (KeyValuePair<string, ThumbnailCacheNode> keyValuePair in this._map)
      {
        if (keyValuePair.Value.ReferenceCount == 0)
          thumbnailCacheNodeList.Add(keyValuePair.Value);
      }
      thumbnailCacheNodeList.Sort((IComparer<ThumbnailCacheNode>) this._nodeComparer);
      int num;
      for (num = 0; this.Count > this._capacity && num < thumbnailCacheNodeList.Count; --this.Count)
      {
        this._map.Remove(thumbnailCacheNodeList[num].Key);
        this._thumbnailCreatorView.CancelRequest(thumbnailCacheNodeList[num].Key);
        if ((NativeObject) thumbnailCacheNodeList[num].Value != (NativeObject) null)
          thumbnailCacheNodeList[num].Value.Release();
        ++num;
      }
      thumbnailCacheNodeList.RemoveRange(0, num);
    }

    public void Add(string key, Texture value)
    {
      ThumbnailCacheNode thumbnailCacheNode1;
      if (this._map.TryGetValue(key, out thumbnailCacheNode1))
      {
        thumbnailCacheNode1.Value = value;
        thumbnailCacheNode1.FrameNo = Utilities.EngineFrameNo;
      }
      else
      {
        ThumbnailCacheNode thumbnailCacheNode2 = new ThumbnailCacheNode(key, value, Utilities.EngineFrameNo);
        this._map[key] = thumbnailCacheNode2;
        ++this.Count;
      }
    }

    public int GetTotalMemorySize()
    {
      int totalMemorySize = 0;
      foreach (ThumbnailCacheNode thumbnailCacheNode in this._map.Values)
        totalMemorySize += thumbnailCacheNode.Value.MemorySize;
      return totalMemorySize;
    }
  }
}
